package la.iok.hzsvn.lewin.movie.controller;

import la.iok.hzsvn.lewin.movie.core.Token;
import la.iok.hzsvn.lewin.movie.core.exception.ErrorCode;
import la.iok.hzsvn.lewin.movie.entity.MovieInstance;
import la.iok.hzsvn.lewin.movie.entity.MovieSceneInstance;
import la.iok.hzsvn.lewin.movie.model.MovieSceneVo;
import la.iok.hzsvn.lewin.movie.service.MovieDownloadService;
import la.iok.hzsvn.lewin.movie.service.MovieInstanceService;
import la.iok.hzsvn.lewin.movie.service.MovieSceneFileService;
import la.iok.hzsvn.lewin.movie.service.MovieSceneInstanceService;
import la.iok.hzsvn.share.response.ListDataResponse;
import la.iok.hzsvn.share.response.SingleDataResponse;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

@RestController
@RequestMapping("movieSceneInstances")
public class MovieSceneInstanceController {
    private final MovieSceneInstanceService movieSceneInstanceService;
    private final MovieSceneFileService movieSceneFileService;
    private final MovieDownloadService movieDownloadService;
    private final MovieInstanceService movieInstanceService;

    public MovieSceneInstanceController(
            MovieSceneInstanceService movieSceneInstanceService,
            MovieSceneFileService movieSceneFileService,
            MovieDownloadService movieDownloadService,
            MovieInstanceService movieInstanceService) {
        this.movieSceneInstanceService = movieSceneInstanceService;
        this.movieSceneFileService = movieSceneFileService;
        this.movieDownloadService = movieDownloadService;
        this.movieInstanceService = movieInstanceService;
    }

    @GetMapping("movie/{movieId}/movieScene/{id}/digest")
    public SingleDataResponse<String> digest(@RequestHeader Token token, @PathVariable Long movieId, @PathVariable Long id){
        MovieSceneInstance movieSceneInstance = movieSceneInstanceService.movieScene(movieDownloadService.loadDownloadInstance(token,movieId),id);
        SingleDataResponse<String> res = new SingleDataResponse<>();
        if(movieSceneInstance != null){
            res.setData(movieSceneFileService.digest(movieSceneInstance.getEncodedFileId()));
        }
        return res;
    }

    @GetMapping("movie/{movieId}/v/{version}/movieScene/{id}/digest")
    public SingleDataResponse<String> digest2(@PathVariable Long movieId, @PathVariable String version, @PathVariable Long id){
        MovieInstance mi = movieInstanceService.movieInst2(movieId,version);
        MovieSceneInstance movieSceneInstance = movieSceneInstanceService.movieScene(mi.getId(),id);
        SingleDataResponse<String> res = new SingleDataResponse<>();
        if(movieSceneInstance != null){
            res.setData(movieSceneFileService.digest(movieSceneInstance.getEncodedFileId()));
        }
        return res;
    }

    @GetMapping("movie/{movieId}/v/{version}/movieScene/{id}/storePath")
    public SingleDataResponse<String> storePath(@PathVariable Long movieId, @PathVariable String version, @PathVariable Long id){
        MovieInstance mi = movieInstanceService.movieInst2(movieId,version);
        MovieSceneInstance movieSceneInstance = movieSceneInstanceService.movieScene(mi.getId(),id);
        SingleDataResponse<String> res = new SingleDataResponse<>();
        if(movieSceneInstance != null){
            res.setData(movieSceneFileService.storePath(movieSceneInstance.getEncodedFileId()));
        }
        return res;
    }

    /**
     * 查询剧本的所有剧幕
     * @param token 操作用户
     * @param id 剧本id
     * @return 剧幕列表
     */
    @GetMapping("movie/{id}")
    public ListDataResponse<MovieSceneVo> scenes(@RequestHeader Token token, @PathVariable Long id){
        List<MovieSceneInstance> list = movieSceneInstanceService.listByProperty("movieInstanceId",
                movieDownloadService.loadDownloadInstance(token,id),"sequence");
        return new ListDataResponse<>(movieSceneInstanceService.po2vo(list));
    }

    /**
     * 查询剧本的所有剧幕
     * @param id 剧本id
     * @return 剧幕列表
     */
    @GetMapping("movie/{id}/v/{version}")
    public ListDataResponse<MovieSceneVo> scenes2(@PathVariable Long id, @PathVariable String version){
        MovieInstance mi = movieInstanceService.movieInst2(id,version);
        List<MovieSceneInstance> list = movieSceneInstanceService.listByProperty("movieInstanceId", mi.getId(),"sequence");
        return new ListDataResponse<>(movieSceneInstanceService.po2vo(list));
    }

    @GetMapping("movie/{movieId}/movieScene/{id}/file")
    public void downloadFile(@RequestHeader Token token,HttpServletResponse response, @PathVariable Long movieId, @PathVariable Long id){
        MovieSceneInstance movieSceneInstance = movieSceneInstanceService.movieScene(
                movieDownloadService.loadDownloadInstance(token,movieId),id);
        ErrorCode.SCENE_NOT_EXIST.assertNotNull(movieSceneInstance,id);
        movieSceneFileService.downloadFile(response,movieSceneInstance.getEncodedFileId(),movieSceneInstance.getName());
    }

    @GetMapping("movie/{movieId}/v/{version}/movieScene/{id}/file")
    public void downloadFile2(HttpServletResponse response, @PathVariable Long movieId, @PathVariable String version, @PathVariable Long id){
        MovieInstance mi = movieInstanceService.movieInst2(movieId,version);
        MovieSceneInstance movieSceneInstance = movieSceneInstanceService.movieScene(mi.getId(),id);
        ErrorCode.SCENE_NOT_EXIST.assertNotNull(movieSceneInstance,id);
        movieSceneFileService.downloadFile(response,movieSceneInstance.getEncodedFileId(),movieSceneInstance.getName());
    }
}
